import Foundation
import UIKit

class ZoomImageView: UIView {
    
    var layoutSubviewsBlock: ((CGRect) -> Void)?

    var image: UIImage! {
        didSet{
            imageView.image = image
            setNeedsLayout()
        }
    }
    
    /// 计算图片适合的size
    private var fitSize: CGSize {
        guard let image = imageView.image else {
            return CGSize.zero
        }
        var width: CGFloat
        var height: CGFloat
        if imageContainer.bounds.width < imageContainer.bounds.height {
            // 竖屏
            let twidth = imageContainer.bounds.width
            let theight = (image.size.height / image.size.width) * twidth
            
            if theight > imageContainer.bounds.height{
                height = imageContainer.bounds.height
                width = (image.size.width / image.size.height) * height
            }else{
                height = theight
                width = twidth
            }
        } else {
            // 横屏
            height = imageContainer.bounds.height
            width = (image.size.width / image.size.height) * height
            if width > imageContainer.bounds.width {
                width = imageContainer.bounds.width
                height = (image.size.height / image.size.width) * width
            }
        }
        
        return CGSize(width: width, height: height)
    }
    
    /// 计算图片复位坐标
    private var resettingCenter: CGPoint {
        let deltaWidth = bounds.width - imageContainer.contentSize.width
        let offsetX = deltaWidth > 0 ? deltaWidth * 0.5 : 0
        let deltaHeight = bounds.height - imageContainer.contentSize.height
        let offsetY = deltaHeight > 0 ? deltaHeight * 0.5 : 0
        return CGPoint(x: imageContainer.contentSize.width * 0.5 + offsetX,
                       y: imageContainer.contentSize.height * 0.5 + offsetY)
    }
    
    lazy var imageContainer: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.backgroundColor = UIColor.black
        scrollView.maximumZoomScale = 4
        scrollView.minimumZoomScale = 1
        scrollView.delegate = self
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()
    
    lazy var imageView: UIImageView = {
        let imgV = UIImageView(frame: .zero)
        imgV.backgroundColor = UIColor.black
        imgV.contentMode = .scaleAspectFit
        imgV.clipsToBounds = true
        imgV.isUserInteractionEnabled = true
        return imgV
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI() {
        self.backgroundColor = UIColor.black
        self.addSubview(imageContainer)
        imageContainer.addSubview(imageView)
        imageContainer.contentInsetAdjustmentBehavior = .never
    }
    
    open override func layoutSubviews() {
        super.layoutSubviews()
        
        imageContainer.frame = self.bounds
        imageContainer.setZoomScale(1.0, animated: false)
        imageView.frame.size = fitSize
        //        ZDebugPrint(fitSize)
        imageView.center = CGPoint(x: imageContainer.bounds.width / 2, y: imageContainer.bounds.height / 2)
        
        layoutSubviewsBlock?(imageView.frame)
    }
}

extension ZoomImageView: UIScrollViewDelegate {
    public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    
    public func scrollViewDidZoom(_ scrollView: UIScrollView) {
        imageView.center = resettingCenter
    }
}

